
public class Atribuicao implements Comando {

	private Variavel variavel;
	private Expressao expressao;
	private String operador;
	
	public Atribuicao(Variavel variavel, Expressao expressao, String operador) {
		this.variavel = variavel;
		this.expressao = expressao;
		this.operador = operador;
	}

	@Override
	public String getCode() {
		Integer regNumber = ParserUtils.novoRegistrador();
		StringBuffer sb = new StringBuffer();
		if (expressao instanceof Literal || expressao instanceof Variavel) {
			sb.append("LD " + "R" + regNumber + ", " + expressao.getCode());
			if (!operador.equals("=")) {
				sb.append(System.lineSeparator());
				Integer regAux = ParserUtils.novoRegistrador();
				sb.append("LD " + "R" + regAux + ", " + variavel.getCode());
				sb.append(System.lineSeparator());
				if (operador.equals("+=")) {
					sb.append("SUM " + "R" + regNumber + ", R" + regAux + ", R" + regNumber);
				} else if (operador.equals("-=")) {
					sb.append("SUB " + "R" + regNumber + ", R" + regAux + ", R" + regNumber);
				} else if (operador.equals("/=")) {
					sb.append("DIV " + "R" + regNumber + ", R" + regAux + ", R" + regNumber);
				} else if (operador.equals("%=")) {
					sb.append("ST " + "R" + regNumber + ", R" + regAux + " MOD R" + regNumber);
				} else if (operador.equals("&=")) {
					sb.append("AND " + "R" + regNumber + ", R" + regAux + ", R" + regNumber);
				} else if (operador.equals("|=")) {
					sb.append("OR " + "R" + regNumber + ", R" + regAux + ", R" + regNumber);
				} else if (operador.equals("^=")) {
					sb.append("XOR " + "R" + regNumber + ", R" + regAux + ", R" + regNumber);
				} else if (operador.equals("*=")) {
					sb.append("MUL " + "R" + regNumber + ", R" + regAux + ", R" + regNumber);
				}
				ParserUtils.liberaRegistradores(regAux);
			}
		} else if (expressao instanceof MatBasica) {
			MatBasica mb = (MatBasica) expressao;
			sb.append(mb.getCode());
			sb.append(System.lineSeparator());
			sb.append("LD R" + regNumber + ", R"+mb.registradorFinal());
		} else if (expressao instanceof IncDecRement) {
			// IncDecRement incDec = (IncDecRement) expressao;
			
			// Em PHP atribuicao pra um increment nao muda o codigo em nada..
			return "";
		} else if (expressao instanceof Deslocamento) {
			Deslocamento mb = (Deslocamento) expressao;
			sb.append(mb.getCode());
			sb.append(System.lineSeparator());
			sb.append("LD R" + regNumber + ", R"+mb.registradorFinal());
		} else if (expressao instanceof Modulo) {
			Modulo mb = (Modulo) expressao;
			sb.append(mb.getCode());
			sb.append(System.lineSeparator());
			sb.append("LD R" + regNumber + ", R"+mb.registradorFinal());
		} else if (expressao instanceof ChamadaFuncao) {
			ChamadaFuncao mb = (ChamadaFuncao) expressao;
			sb.append(mb.getCode());
			sb.append(System.lineSeparator());
			sb.append("LD R" + regNumber + ", R"+mb.registradorFinal());
		}
		sb.append(System.lineSeparator());
		sb.append("ST " + variavel.getCode() + ", R" + regNumber);
		ParserUtils.liberaRegistradores(regNumber);
		return sb.toString();
	}
	
}
